const db=require('../utils/mysql')
const { v4: uuidv4 } = require('uuid');
module.exports={
    repairAdd:async (ctx,next)=>{
        let query=ctx.request.body
        console.log(query)
        let repairId=uuidv4()
        let result= await new Promise((resolve,reject)=>{
            const sql=`INSERT INTO repair (repairId,personId,name,phone,ordertime,region,address,repairtype,content,photos,repairstatus,sumbittime,nickname) 
             values ('${repairId}','${query.personId}','${query.name}','${query.phone}','${query.ordertime}','${query.region}','${query.address}',
            '${query.repairtype}','${query.content}','${query.photos}',1,'${query.sumbittime}','${query.nickname}')`
            db(sql,(err,data)=>{
                if(err) reject(err);
                resolve({
                    msg:'提交成功',
                    status:200
                })
            })
        })
        ctx.body=result
    },
    getRepairInfo:async (ctx,next)=>{
        let query=ctx.request.query
        let result= await new Promise((resolve,reject)=>{
            const sql=`select * from repair where repairId='${query.repairId}'`
            db(sql,(err,data)=>{
                if(err) reject(err);
                resolve({
                    msg:'报修单详情获取成功',
                    status:200,
                    data:data
                })
            })
        })
        ctx.body=result
    },

    repairCancel:async (ctx,next)=>{
        let query=ctx.request.body
        console.log(query.repairId)
        let result= await new Promise((resolve,reject)=>{
            const sql=`update repair set repairstatus=0 where repairId='${query.repairId}'`
            db(sql,(err,data)=>{
                if(err) reject(err);
                resolve({
                    msg:'取消成功',
                    status:200
                })
            })
        })
        ctx.body=result
    },
    repairFast:async (ctx,next)=>{
        let query=ctx.request.body
        let result= await new Promise((resolve,reject)=>{
            const sql=`update repair set isfast=1 where repairId='${query.repairId}'`
            db(sql,(err,data)=>{
                if(err) reject(err);
                resolve({
                    msg:'催单成功',
                    status:200
                })
            })
        })
        ctx.body=result
    },
    getRepairList:async (ctx,next)=>{
        let query=ctx.request.query

        let sql=''
        if (ctx.identity=='repairer'){//维修工
            if (query.repairstatus==6){
                sql=`select * from repair where maintainerId='${ctx.personId}'`
            }else if(query.repairstatus==2){
                // sql=`select * from repair where maintainerId='${ctx.personId}'`
                sql=`select * from repair where maintainerId='${ctx.personId}'and repairstatus='2'`
            }else if(query.repairstatus==4){
                sql=`select * from repair where maintainerId='${ctx.personId}'and repairstatus='4'`
            }else if(query.repairstatus==5){
                sql=`select * from repair where maintainerId='${ctx.personId}'and repairstatus='5'`
            }

        }else if (ctx.identity=='admin'){//管家
            if (query.repairstatus==6){
                sql=`select * from repair`
            }else if(query.repairstatus==1){
                sql=`select * from repair where repairstatus=1`
            }else if(query.repairstatus==4){
                sql=`select * from repair where repairstatus=4`
            }

        }else { //用户
            sql=`select * from repair where personId='${ctx.personId}' and repairstatus=${query.repairstatus}`
        }
        console.log(sql)
        let result= await new Promise((resolve,reject)=>{
            db(sql,(err,data)=>{
                if(err) reject(err);
                resolve({
                    msg:'报修单获取成功',
                    status:200,
                    data:data
                })
            })
        })
        ctx.body=result

    },
    personAssign:async (ctx,next)=>{
        let query=ctx.request.body
        console.log(query)
        let result= await new Promise((resolve,reject)=>{
            const sql=`update repair set repairstatus=2,maintainerId='${query.maintainerId}',maintainername='${query.maintainername}' where repairId='${query.repairId}'`
            db(sql,(err,data)=>{
                if(err) reject(err);
                resolve({
                    msg:'指派成功',
                    status:200,
                })
            })
        })
        ctx.body=result
    },
    beginRepair:async (ctx,next)=>{
        let query=ctx.request.body
        console.log(query.repairId)
        let result= await new Promise((resolve,reject)=>{
            const sql=`update repair set repairstatus=3 where repairId='${query.repairId}'`
            db(sql,(err,data)=>{
                if(err) reject(err);
                resolve({
                    msg:'开始维修成功',
                    status:200,
                })
            })
        })
        ctx.body=result
    },
    repersonAssign:async (ctx,next)=>{
        let query=ctx.request.body
        console.log(query.repairId)
        let result= await new Promise((resolve,reject)=>{
            const sql=`update repair set repairstatus=1 where repairId='${query.repairId}'`
            db(sql,(err,data)=>{
                if(err) reject(err);
                resolve({
                    msg:'打回改派成功',
                    status:200,
                })
            })
        })
        ctx.body=result
    },
    repairIdea:async (ctx,next)=>{
        let query=ctx.request.query
        let result= await new Promise((resolve,reject)=>{
            const sql=`update repair set personevaluate='${query.personevaluate}' where repairId='${query.repairId}'`
            db(sql,(err,data)=>{
                if(err) reject(err);
                resolve({
                    msg:'评价成功',
                    status:200,
                })
            })
        })
        ctx.body=result

    },
    repairReport:async (ctx,next)=>{
        let query=ctx.request.body
        console.log(query.result)
        let result= await new Promise((resolve,reject)=>{
            const sql=`update repair set result='${query.result}',repairstatus=4 where repairId='${query.repairId}'`
            db(sql,(err,data)=>{
                if(err) reject(err);
                resolve({
                    msg:'维修结果上报成功',
                    status:200,
                })
            })
        })
        ctx.body=result

    },
    repairAcceptance:async (ctx,next)=>{
        let query=ctx.request.body
        console.log(query.repairstatus)
        let result= await new Promise((resolve,reject)=>{
            const sql=`update repair set 
            acceptanceidea='${query.acceptanceidea}',
            repairstatus=${query.repairstatus},
            acceptanceId='${query.acceptanceId}',
            acceptance='${query.acceptance}'
             where repairId='${query.repairId}'`
            db(sql,(err,data)=>{
                if(err) reject(err);
                resolve({
                    msg:'验收操作成功',
                    status:200,
                })
            })
        })
        ctx.body=result
    },

}

